Skip to content

[ConstraintElim] Preserve analyses when IR is unchanged. #128588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2025

Conversation

andjo403
Copy link
Contributor

Noticed that all Analys passes was not preserved for unchanged IR when the test was containing assumes.

PreservedAnalyses::all() is not called for E.g. https://github.com/andjo403/llvm-project/blob/e24bb833b1af521fc2aec997a8b89de795b34449/llvm/test/Transforms/ConstraintElimination/mul.ll#L283-L301

Not sure how to/if possible to add a test for this.

@llvmbot
Copy link
Member

llvmbot commented Feb 24, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Andreas Jonson (andjo403)

Changes

Noticed that all Analys passes was not preserved for unchanged IR when the test was containing assumes.

PreservedAnalyses::all() is not called for E.g. https://github.com/andjo403/llvm-project/blob/e24bb833b1af521fc2aec997a8b89de795b34449/llvm/test/Transforms/ConstraintElimination/mul.ll#L283-L301

Not sure how to/if possible to add a test for this.


Full diff: https://github.com/llvm/llvm-project/pull/128588.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/ConstraintElimination.cpp (+7-4)
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 79f6858463d7e..267eb319a5616 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -1435,8 +1435,9 @@ static bool checkAndReplaceCondition(
     generateReproducer(Cmp, ReproducerModule, ReproducerCondStack, Info, DT);
     Constant *ConstantC = ConstantInt::getBool(
         CmpInst::makeCmpResultType(Cmp->getType()), IsTrue);
-    Cmp->replaceUsesWithIf(ConstantC, [&DT, NumIn, NumOut,
-                                       ContextInst](Use &U) {
+    bool Changed = false;
+    Cmp->replaceUsesWithIf(ConstantC, [&DT, NumIn, NumOut, ContextInst,
+                                       &Changed](Use &U) {
       auto *UserI = getContextInstForUse(U);
       auto *DTN = DT.getNode(UserI->getParent());
       if (!DTN || DTN->getDFSNumIn() < NumIn || DTN->getDFSNumOut() > NumOut)
@@ -1448,12 +1449,14 @@ static bool checkAndReplaceCondition(
       // Conditions in an assume trivially simplify to true. Skip uses
       // in assume calls to not destroy the available information.
       auto *II = dyn_cast<IntrinsicInst>(U.getUser());
-      return !II || II->getIntrinsicID() != Intrinsic::assume;
+      bool ShouldReplace = !II || II->getIntrinsicID() != Intrinsic::assume;
+      Changed |= ShouldReplace;
+      return ShouldReplace;
     });
     NumCondsRemoved++;
     if (Cmp->use_empty())
       ToRemove.push_back(Cmp);
-    return true;
+    return Changed;
   };
 
   if (auto ImpliedCondition =

Copy link
Contributor

@fhahn fhahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how to/if possible to add a test for this.

You might be able to use something like

opt -S -passes='require<scalar-evolution>,constraint-elimination' -debug-pass-manager to check that scalar-evolution is preserved with the change

Copy link
Contributor

@antoniofrighetto antoniofrighetto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

opt -S -passes='require<scalar-evolution>,constraint-elimination' -debug-pass-manager to check that scalar-evolution is preserved with the change

Looks like scalar evolution is currently already in the set of the preserved analyses.

@fhahn
Copy link
Contributor

fhahn commented Feb 25, 2025

LGTM, thanks!

opt -S -passes='require<scalar-evolution>,constraint-elimination' -debug-pass-manager to check that scalar-evolution is preserved with the change

Looks like scalar evolution is currently already in the set of the preserved analyses.

Then we should use an analysis that doesn't get preserved even when changes are made.

andjo403 added a commit that referenced this pull request Feb 25, 2025
@andjo403 andjo403 force-pushed the ConstraintEliminationPreservedAnalyses branch from cf3be5b to ed9c629 Compare February 25, 2025 12:09
@andjo403
Copy link
Contributor Author

test added

Copy link
Contributor

@fhahn fhahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@andjo403 andjo403 merged commit 4b29c28 into llvm:main Feb 25, 2025
9 of 10 checks passed
@andjo403 andjo403 deleted the ConstraintEliminationPreservedAnalyses branch February 25, 2025 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants